package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.CommunityMapper;
import cn.tedu.property.management.pojo.dto.CommunityAddNewDTO;
import cn.tedu.property.management.pojo.dto.CommunityUpdateDTO;
import cn.tedu.property.management.pojo.vo.CommunityStandardVO;
import cn.tedu.property.management.pojo.entity.Community;
import cn.tedu.property.management.service.ICommunityService;
import cn.tedu.property.management.web.ServiceCode;
import com.fasterxml.jackson.databind.util.ViewMatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommunityServiceImpl implements ICommunityService {
    @Autowired
    private CommunityMapper communityMapper;
    @Override
    public void insert(CommunityAddNewDTO communityAddNewDTO) {
        int i = communityMapper.countCommunityByName(communityAddNewDTO.getName());
        if(i>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"小区名称已存在！");
        }
        int o = communityMapper.countCommunityByAddress(communityAddNewDTO.getAddress());
        if(o>0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"小区地址已存在！");
        }
        Community community = new Community();
        BeanUtils.copyProperties(communityAddNewDTO,community);
        int rows = communityMapper.insert(community);
        if(rows!=1){
            String message = "添加失败，服务器繁忙，请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        CommunityStandardVO community = communityMapper.getCommunityById(id);
        if(community==null){
            String message = "删除失败，尝试删除的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int rows = communityMapper.deleteById(id);
        if(rows!=1){
            String message = "删除失败，服务器繁忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateById(Long id, CommunityUpdateDTO communityUpdateDTO) {
        CommunityStandardVO communityById = communityMapper.getCommunityById(id);
        if(communityById==null){
            String message = "更新失败，尝试更新的数据不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Community community = new Community();
        BeanUtils.copyProperties(communityUpdateDTO,community);
        community.setId(id);
        int rows = communityMapper.updateById(community);
        if(rows!=1){
            String message = "更新失败，服务器繁忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public CommunityStandardVO getCommunityById(Long id) {
        CommunityStandardVO communityById = communityMapper.getCommunityById(id);
        if(communityById==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"您要查询的小区不存在！");
        }
        return communityById;
    }

    @Override
    public List<CommunityStandardVO> getCommunity() {
        return communityMapper.getCommunity();
    }
}
